package com.jcfk.eam.service.base;


import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamPersonLocationPowerDao;
import com.jcfk.eam.domain.dto.base.EamPersonLocationPowerDTO;
import com.jcfk.eam.domain.po.base.EamPersonAssetPower;
import com.jcfk.eam.domain.po.base.EamPersonLocationPower;
import com.jcfk.eam.domain.query.base.EamPersonLocationPowerQuery;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 人员位置管理权限
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-08-20
 */
@Service
public class EamPersonLocationPowerService extends BaseService<EamPersonLocationPowerDao, EamPersonLocationPower> {

    /**
     * 判断EamPersonLocationPower是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamPersonLocationPowerDTO dto) {
        QueryWrapper<EamPersonLocationPower> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入人员位置管理权限
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamPersonLocationPowerDTO dto) {

        // 保存
        EamPersonLocationPower item = new EamPersonLocationPower();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新人员位置管理权限
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamPersonLocationPowerDTO dto) {

        // 保存
        EamPersonLocationPower item = new EamPersonLocationPower();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveList(String personPowerId,List<EamPersonLocationPowerDTO> dtoList){
        List<EamPersonLocationPower> list=convertList(dtoList, EamPersonLocationPower.class);

        //删除原数据
        this.deleteList(personPowerId);

        for(EamPersonLocationPower item : list){
            setDefaultValue(item);
        }

        //保存新数据
        super.saveBatch(list);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamPersonLocationPower item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取人员位置管理权限信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamPersonLocationPowerDTO get(String id) {
        // 查询数据
        EamPersonLocationPower item = this.baseMapper.selectById(id);
        EamPersonLocationPowerDTO dto = new EamPersonLocationPowerDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamPersonLocationPowerDTO>
     */
    public List<EamPersonLocationPowerDTO> getList(EamPersonLocationPowerQuery query) {
        // 查询条件
        QueryWrapper<EamPersonLocationPower> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getPersonPowerId()), EamPersonLocationPower::getPersonPowerId, query.getPersonPowerId());
        // 查询数据
        List<EamPersonLocationPower> list = super.list(qw);

        return convertList(list, EamPersonLocationPowerDTO.class);
    }

    /**
     * 批量删除
     * @param personPowerId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteList(String personPowerId){
        // 查询条件
        QueryWrapper<EamPersonLocationPower> qw = new QueryWrapper<>();
        qw.lambda().eq(EamPersonLocationPower::getPersonPowerId, personPowerId);
        this.baseMapper.delete(qw);
    }

    /**
     * 根据主键获取人员位置管理权限信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        this.removeById(id);
    }
}

